package com.topway.dsmflex.ui.dialog;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import com.swtdesigner.SWTResourceManager;


/**
 * 
 * @author liusy 2013-05-21
 *
 */
public class AboutDialog extends Dialog {

   private StyledText styledText;
   protected Object   result;
   protected Shell    shell;

   
   /**
    * Create the dialog
    * 
    * @param parent
    * @param style
    */
   public AboutDialog(Shell parent, int style) {
      super(parent, style);
   }

   /**
    * Create the dialog
    * 
    * @param parent
    */
   public AboutDialog(Shell parent) {
      this(parent, SWT.NONE);
   }

   /**
    * Open the dialog
    * 
    * @return the result
    */
   public Object open() {
      createContents();
      Display display = getParent().getDisplay();
      setPos(display);
      shell.open();
      shell.layout();

      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) display.sleep();
      }
      return result;
   }

   /**
    * Create contents of the dialog
    */
   protected void createContents() {
      shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
      shell.setImage(SWTResourceManager.getImage(AboutDialog.class, "/img/2010060902463514_easyicon_cn_48.png"));
      shell.setLayout(new FormLayout());
      shell.setSize(500, 255);
      shell.setText("关于需求侧数据同步系统");

      final Button btnClose = new Button(shell, SWT.NONE);
      btnClose.addSelectionListener(new SelectionAdapter() {
         public void widgetSelected(final SelectionEvent e) {
            shell.dispose();
         }
      });
      final FormData fd_btnClose = new FormData();
      fd_btnClose.width = 70;
      fd_btnClose.left = new FormAttachment(50, -35);
      fd_btnClose.bottom = new FormAttachment(100, -5);
      btnClose.setLayoutData(fd_btnClose);
      btnClose.setText("关闭");

      final Composite composite = new Composite(shell, SWT.NONE);
      composite.setLayout(new FormLayout());
      final FormData fd_composite = new FormData();
      fd_composite.bottom = new FormAttachment(100, -35);
      fd_composite.top = new FormAttachment(0, 0);
      fd_composite.left = new FormAttachment(0, 1);
      fd_composite.right = new FormAttachment(100, -1);
      composite.setLayoutData(fd_composite);

      final Group group = new Group(composite, SWT.NONE);
      group.setLayout(new FormLayout());
      final FormData fd_group = new FormData();
      fd_group.left = new FormAttachment(0, 1);
      fd_group.bottom = new FormAttachment(100, -1);
      fd_group.top = new FormAttachment(0, -5);
      fd_group.right = new FormAttachment(100, -1);
      group.setLayoutData(fd_group);

      final Label label = new Label(group, SWT.NONE);
      label.setForeground(SWTResourceManager.getColor(0, 0, 255));
      final FormData fd_label = new FormData();
      fd_label.top = new FormAttachment(30, 0);
      fd_label.left = new FormAttachment(30, 0);
      label.setLayoutData(fd_label);
      label.setFont(SWTResourceManager.getFont("宋体", 18, SWT.BOLD));
      label.setText("需求侧数据同步系统");

      styledText = new StyledText(group, SWT.READ_ONLY);
      styledText.setForeground(SWTResourceManager.getColor(255, 0, 0));
      final FormData fd_styledText = new FormData();
      fd_styledText.left = new FormAttachment(30, 5);
      fd_styledText.bottom = new FormAttachment(100, -5);
      styledText.setLayoutData(fd_styledText);
      styledText.setText("Copyright(C)  2013  广东卓维");
      styledText.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));

      final Label label_1 = new Label(group, SWT.NONE);
      label_1.setFont(SWTResourceManager.getFont("宋体", 12, SWT.NONE));
      final FormData fd_label_1 = new FormData();
      fd_label_1.left = new FormAttachment(label, 2, SWT.LEFT);
      fd_label_1.bottom = new FormAttachment(label, -5, SWT.TOP);
      label_1.setLayoutData(fd_label_1);
      label_1.setText("广东卓维数据同步系统");

      final Label label_2 = new Label(group, SWT.NONE);
      label_2.setFont(SWTResourceManager.getFont("宋体", 10, SWT.BOLD));
      final FormData fd_label_2 = new FormData();
      fd_label_2.left = new FormAttachment(label, 2, SWT.LEFT);
      fd_label_2.top = new FormAttachment(label, 5);
      label_2.setLayoutData(fd_label_2);
      label_2.setText("版本：1.0.2");

      final Label label_3 = new Label(group, SWT.NONE);
      label_3.setImage(SWTResourceManager.getImage(AboutDialog.class, "/img/welcome.png"));
      final FormData fd_label_3 = new FormData();
      fd_label_3.top = new FormAttachment(0, 10);
      fd_label_3.left = new FormAttachment(0, 5);
      label_3.setLayoutData(fd_label_3);

      final Label label_4 = new Label(group, SWT.NONE);
      label_4.setImage(SWTResourceManager.getImage(AboutDialog.class, "/img/logo_ico.PNG"));
      final FormData fd_label_4 = new FormData();
      fd_label_4.top = new FormAttachment(0, -4);
      fd_label_4.right = new FormAttachment(100, -2);
      label_4.setLayoutData(fd_label_4);
      //
   }

   private void setPos(Display display) {
      Rectangle rec = Display.getCurrent().getClientArea();
      shell.setLocation((rec.width - shell.getSize().x) / 2, (rec.height - shell.getSize().y) / 2);
   }

}
